#include "unistd.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#include <sys/mman.h>
#include <sys/types.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <com_example_gpio_app_Gpio_Jni.h>

#define SDRAM_PHY_Position 0xc0000000
char	io_table_fix01[][4] = {
        "132", 	//GPE4	ok	I2SSDO	R26
        "131", 	//GPE3	ok
        "129", 	//GPE1	ok
        "128",	//GPE0	ok
        "130", 	//GPE2	ok

        "32", 	//GPB0	ok
        "34", 	//GPB2	ok
        "33", 	//GPB1	ok
//	"35",  	//GPB3	ok		LCD_BKL	D28

        "236",	//GPH12	ok		R99

        "233",	//GPH9	ok 		R25
        "232",	//GPH8	ok		R28
        "224", //GPH0	ok	TXD0
        "225", //GPH1	ok
        "226", 	//GPH2	ok	TXD1
        "227", 	//GPH3	ok
        "228", 	//GPH4	ok		R43

        "229", 	//GPH5	ok		R46
        "230", 	//GPH6	ok		R49
        "231", 	//GPH7	RXD3 	R52	ok

        "142",		//GPE14		I2c
        "143",		//GPE15

        "141",		//GPE13 ok		R61
        "139",		//GPE11 ok		R64
        "140",		//GPE12 ok		R67

        "333", 		//GPL13	CAN_CS R70

        "198", 		//GPG6	CAN_INT R73 ok
};

char	io_table_fix02[][4] = {
        "234", //GPH10	ok
        "238", //GPH14	ok
        "237", //GPH13 R84	ok

        "197", //GPG5	ok
        "196", //GPG4	ok
        "195", //GPG3	ok
        "194", //GPG2	ok
        "193", //GPG1	ok
        "192", //GPG0	EINT9	R69 ok

        "167",	//GPF7	ok
        "166", //GPF6	ok
        "165", //GPF5	ok
        "164", //GPF4	ok
        "163", //GPF3	ok
        "162", //GPF2	ok
        "161", //GPF1	ok
        "160", //GPF0	EINT0	R42 ok

        "137", //GPE9		ok
        "138", //GPE10	ok
        "134", //GPE6	ok
        "133", //GPE5	ok
        "135", //GPE7	ok
        "136", //GPE8	SD0_DAT1	R24 ok		SD0_DAT1
};

int	calc_port_num(char port, int num)
{
    int	port_num;

    switch(port){
        case 'a':
        case 'A':
            port_num = num;
            break;
        case 'b':
        case 'B':
            port_num = 32 + num;
            break;
        case 'c':
        case 'C':
            port_num = 64 + num;
            break;
        case 'd':
        case 'D':
            port_num = 96 + num;
            break;
        case 'e':
        case 'E':
            port_num = 128 + num;
            break;
        case 'f':
        case 'F':
            port_num = 160 + num;
            break;
        case 'g':
        case 'G':
            port_num = 192 + num;
            break;
        case 'h':
        case 'H':
            port_num = 224 + num;
            break;
        case 'i':
        case 'I':
            port_num = 256 + num;
            break;
        case 'j':
        case 'J':
            port_num = 288 + num;
            break;
        case 'K':
        case 'k':
            port_num = 320 + num;
            break;
        case 'l':
        case 'L':
            port_num = 352 + num;
            break;
        case 'm':
        case 'M':
            port_num = 384 + num;
            break;
        case 'n':
        case 'N':
            port_num = 416 + num;
            break;
        default:
            port_num = 0;
    }
    return port_num;
}


JNIEXPORT void JNICALL Java_com_example_gpio_1app_Gpio_1Jni_gpioOutput
(JNIEnv *env, jobject jobj, jchar port, jint num, jint val)
{
    int	port_num;
    FILE *p=NULL;
    char str[256];
    printf("---------------port:%c,num:%d,val:%d--------------------\n",port,num,val);
    port_num = calc_port_num(port, num);
    p = fopen("/sys/class/gpio/export","w");
    fprintf(p,"%d",port_num);
    fclose(p);
    sprintf(str, "/sys/class/gpio/gpio%d/direction", port_num);
    p = fopen(str,"w");
    fprintf(p,"out");
    fclose(p);

    sprintf(str, "/sys/class/gpio/gpio%d/value", port_num);
    p = fopen(str,"w");
    fprintf(p,"%d",val > 0 ? 1 : 0);
    fclose(p);
}


JNIEXPORT jint JNICALL Java_com_example_gpio_1app_Gpio_1Jni_gpioInput
        (JNIEnv *env, jobject jobj, jchar port, jint num)
{
    int	port_num;
    FILE *p=NULL;
    int	val = -1;
    char str[256];
    printf("---------------(port:%c,num:%d)-------------------\n",port,num);
    port_num = calc_port_num(port, num);
    p = fopen("/sys/class/gpio/export","w");
    fprintf(p,"%d",port_num);
    fclose(p);
    sprintf(str, "/sys/class/gpio/gpio%d/direction", port_num);
    p = fopen(str,"w");
    fprintf(p,"in");
    fclose(p);

    sprintf(str, "/sys/class/gpio/gpio%d/value", port_num);
    p = fopen(str,"r");
    fscanf(p, "%d", &val);
    fclose(p);
    printf("---------------val:%d-------------------\n",val);
    return (jint)val;
}